package com.it.tydic.estate.module.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.it.tydic.estate.common.aop.CodeMsg;
import com.it.tydic.estate.common.aop.Result;
import com.it.tydic.estate.common.util.InstaIdGenerator;
import com.it.tydic.estate.common.util.StringUtils;
import com.it.tydic.estate.common.util.TimeUtil;
import com.it.tydic.estate.module.mapper.inner.*;
import com.it.tydic.estate.module.model.inner.*;
import com.it.tydic.estate.module.service.WpkcService;
import com.it.tydic.estate.module.service.WplbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.util.*;

@Transactional
@Service
public class WpkcServiceImpl implements WpkcService {

    @Autowired
    private TbCkglWpkcMapper wpkcMapper;
    @Autowired
    private TbCkglCkdaMapper ckdaMapper;
    @Autowired
    private TbCkglWplbMapper wplbMapper;
    @Autowired
    private TbCkglGysMapper gysMapper;

    @Autowired
    private TbCkglRkckglMapper rkckglMapper;
    /**
     * 列表页面查询
     * @param map
     * @return
     */
    @Override
    public Result pageList(Map<String,Object> map) {
        if(isNotNull(map)){//参数验证
            Map<String,Object> dataMap = new HashMap<>();
            Example example = new Example(TbCkglWpkc.class);
            //setExample(example,map);//添加查询条件

            //Page<TbCkglWpkc> pageList = (Page<TbCkglWpkc>) wpkcMapper.selectByExample(example);
            if(map.get("szck") == null || map.get("szck").equals("") ){
                Example ckExample = new Example(TbCkglCkda.class);
                ckExample.createCriteria().andEqualTo("userId",String.valueOf(map.get("userId")));
                List<TbCkglCkda> tbCkglCkdas = ckdaMapper.selectByExample(ckExample);
                List<String> ckIds = new ArrayList<>();
                for (TbCkglCkda tbCkglCkda : tbCkglCkdas) {
                    ckIds.add(tbCkglCkda.getId());
                }
                if(ckIds.size()==0){
                    return Result.success(dataMap);
                }
                map.put("ckIds",ckIds);
            }
            PageHelper.startPage((Integer) map.get("pageNum"),(Integer)map.get("pageSize"));
            Page<TbCkglWpkc> pageList =  wpkcMapper.pageList(map);
            dataMap.put("data",pageList.getResult());
            dataMap.put("totle",pageList.getTotal());
            return Result.success(dataMap);
        }
        return Result.error(CodeMsg.INTER_ERROR);
    }



    private void setExample(Example example, Map<String, Object> map) {
        Example.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotEmpty(String.valueOf(map.get("szck")))){
            criteria.andEqualTo("ckId",String.valueOf(map.get("szck")));
        }else {
            Example ckExample = new Example(TbCkglCkda.class);
            ckExample.createCriteria().andEqualTo("userId",String.valueOf(map.get("userId")));
            List<TbCkglCkda> tbCkglCkdas = ckdaMapper.selectByExample(ckExample);
            List<String> ckIds = new ArrayList<>();
            for (TbCkglCkda tbCkglCkda : tbCkglCkdas) {
                ckIds.add(tbCkglCkda.getId());
            }
            criteria.andIn("ckId",ckIds);
        }
        if(StringUtils.isNotEmpty(String.valueOf(map.get("wpdm")))){
            criteria.andLike("wpdm","%"+String.valueOf(map.get("wpdm")+"%"));
        }
        if(StringUtils.isNotEmpty(String.valueOf(map.get("wpmc")))){
            criteria.andLike("wpmc","%"+String.valueOf(map.get("wpmc"))+"%");
        }
        if(StringUtils.isNotEmpty(String.valueOf(map.get("wpxh")))){
            criteria.andLike("wpxh","%"+String.valueOf(map.get("wpxh"))+"%");
        }
        if(StringUtils.isNotEmpty(String.valueOf(map.get("wplb")))){
            criteria.andLike("wplb","%"+String.valueOf(map.get("wplb"))+"%");
        }
        if(StringUtils.isNotEmpty(String.valueOf(map.get("dw")))){
            criteria.andLike("dw","%"+String.valueOf(map.get("dw"))+"%");
        }

    }

    /**
     *验证map参数
     */
    private Boolean isNotNull(Map<String,Object> map) {
        if(StringUtil.isEmpty(String.valueOf(map.get("pageNum")) )|| StringUtil.isEmpty(String.valueOf(map.get("pageSize")))){
            return false;
        }
        return true;
    }

    /**
     * 物品库存列表下拉框
     */
    @Override
    public Result getDataList(String userId) {
        Map<String,Object>dataMap = new HashMap<>();
        if(StringUtil.isEmpty(userId)){
            return Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }
        //所在仓库
        List<Map<String,Object>>szckdataMap=new ArrayList<>();
        Example ckdaExample = new Example(TbCkglCkda.class);
        ckdaExample.createCriteria().andEqualTo("userId",userId).andEqualTo("zfbj","0");
        List<TbCkglCkda> tbCkglCkdas = ckdaMapper.selectByExample(ckdaExample);
        for (TbCkglCkda tbCkglCkda : tbCkglCkdas) {
            Map<String,Object>szckMap = new HashMap<>();
            szckMap.put("key",tbCkglCkda.getId());
            szckMap.put("value",tbCkglCkda.getCkmc());
            szckdataMap.add(szckMap);
        }
        dataMap.put("szck",szckdataMap);
        //物品类别
        List<Map<String,Object>>wplbdataMap=new ArrayList<>();
        Example wplbExample = new Example(TbCkglWplb.class);
        wplbExample.createCriteria().andEqualTo("userId",userId).andEqualTo("zfbj","0");
        List<TbCkglWplb> tbCkglWplbs = wplbMapper.selectByExample(wplbExample);
        for (TbCkglWplb wplb : tbCkglWplbs) {
            Map<String,Object>wplbMap = new HashMap<>();
            wplbMap.put("key",wplb.getId());
            wplbMap.put("value",wplb.getWplbmc());
            wplbdataMap.add(wplbMap);
        }
        dataMap.put("wplb",wplbdataMap);
        //供应商
        List<Map<String,Object>>gysbdataMap=new ArrayList<>();
        Example gysExample = new Example(TbCkglGys.class);
        gysExample.createCriteria().andEqualTo("userId",userId).andEqualTo("zfbj","0");
        List<TbCkglGys> tbCkglgys = gysMapper.selectByExample(gysExample);
        for (TbCkglGys gys : tbCkglgys) {
            Map<String,Object>wplbMap = new HashMap<>();
            wplbMap.put("key",gys.getId());
            wplbMap.put("value",gys.getGysmc());
            gysbdataMap.add(wplbMap);
        }
        dataMap.put("gys",gysbdataMap);
        return Result.success(dataMap);
    }

    /**
     * 单据数据查询
     * @param map
     * @return
     */
    @Override
    public Result danJupageList(Map<String, Object> map) {
        if(isNotNull(map)){//参数验证
            Map<String,Object> dataMap = new HashMap<>();
            Example example = new Example(TbCkglRkckgl.class);
            //setDanJuExample(example,map);//添加查询条件
            PageHelper.startPage((Integer) map.get("pageNum"),(Integer)map.get("pageSize"));
            Page<TbCkglRkckgl> pageList = rkckglMapper.danJupageList(map);
            dataMap.put("data",pageList.getResult());
            dataMap.put("totle",pageList.getTotal());
            return Result.success(dataMap);
        }
        return Result.error(CodeMsg.INTER_ERROR);
    }



    /**
     * 查询单据条件
     * @param example
     * @param map
     */
    private void setDanJuExample(Example example, Map<String, Object> map) {
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("zfbj","0").andEqualTo("userId",map.get("userId"));
        if(StringUtils.isNotEmpty(String.valueOf(map.get("szck")))){
            criteria.andEqualTo("szck",String.valueOf(map.get("szck")));
        }
        if(StringUtils.isNotEmpty(String.valueOf(map.get("czlb")))){
            criteria.andEqualTo("djlb",String.valueOf(map.get("czlb")));
        }
        if(StringUtils.isNotEmpty(String.valueOf(map.get("jsr")))){
            criteria.andLike("jsr","%"+String.valueOf(map.get("jsr"))+"%");
        }
        if(StringUtils.isNotEmpty(String.valueOf(map.get("zdr")))){
            criteria.andLike("zdr","%"+String.valueOf(map.get("zdr"))+"%");
        }
        if(StringUtils.isNotEmpty(String.valueOf(map.get("shzt")))){
            criteria.andEqualTo("shzt",String.valueOf(map.get("shzt")));
        }
        if(StringUtils.isNotEmpty(String.valueOf(map.get("czqsrq")))){
            criteria.andGreaterThanOrEqualTo("fsrq",String.valueOf(map.get("czqsrq")));
        }
        if(StringUtils.isNotEmpty(String.valueOf(map.get("czjzrq")))){
            criteria.andLessThanOrEqualTo("fsrq",String.valueOf(map.get("czjzrq")));
        }
    }

    /**
     * 单据明细列表查询
     * @param map
     * @return
     */
    @Override
    public Result MingXiPageList(Map<String, Object> map) {
        if(isNotNull(map)){//参数验证
            Map<String,Object> dataMap = new HashMap<>();
            Page<Map<String, Object>>pageList = rkckglMapper.selectByRkmxList(map);
            dataMap.put("data",pageList.getResult());
            dataMap.put("totle",pageList.getTotal());
            return Result.success(dataMap);
        }
        return Result.success();
    }

    /**
     * 出库入库单据审核
     * @param tbCkglRkckgl
     * @return
     */
    @Override
    public Result checkByCrkDj(TbCkglRkckgl tbCkglRkckgl) {
        if(tbCkglRkckgl!=null && StringUtil.isNotEmpty(tbCkglRkckgl.getId())){
            rkckglMapper.updateByPrimaryKeySelective(tbCkglRkckgl);
            return Result.success();
        }
        return Result.error(CodeMsg.EMPTY_PARAM_ERROR);
    }
}
